package LeetCode.leetcode1047;

import java.util.Stack;

public class Main {
    public String removeDuplicates(String s) {
        char c;
        StringBuilder sb = new StringBuilder();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (stack.isEmpty() || stack.peek() != c){
                stack.push(c);
                continue;
            }
            if (stack.peek() == c){
                stack.pop();
            }
        }
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }
}
